/** 
 * @date 06/05/2013
 * @author Renet_Pourriau
 * @version 1.0
 */

package temporel;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * This class represent recurrent event
 */
public class EvennementRecurrent {

	private String libelle;

	private EnumPeriodicite periodicite;

	private DateC dateDepart;

	private CreneauHoraire creneau;

	/**
	 * this is the constructor of recurrent event
	 * @param periodicite
	 * @param dateEvenement
	 */
	public EvennementRecurrent(String libelle, EnumPeriodicite periodicite,
			DateC dateEvenement, CreneauHoraire creneau) {
		if (creneau.estCorrect(creneau.getHeureDebut(), creneau.getHeureFin())
				&& dateEvenement.verifDate(dateEvenement.getNumJour(),
						dateEvenement.getMois(), dateEvenement.getAnnee())) {
			this.setLibelle(libelle);
			this.setPeriodicite(periodicite);
			this.setDateEvenement(dateEvenement);
			this.setCreneau(creneau);
		}
	}

	/**
	 * @return the libelle
	 */
	public String getLibelle() {
		return libelle;
	}

	/**
	 * @param libelle
	 *            the libelle to set
	 */
	public void setLibelle(String libelle) {
		this.libelle = libelle;
	}

	/**
	 * @return the periodicite
	 */
	public EnumPeriodicite getPeriodicite() {
		return periodicite;
	}

	/**
	 * @param periodicite
	 *            the periodicite to set
	 */
	public void setPeriodicite(EnumPeriodicite periodicite) {
		this.periodicite = periodicite;
	}

	/**
	 * @return the dateEvenement
	 */
	public DateC getDateEvenement() {
		return this.dateDepart;
	}

	/**
	 * @param dateEvenement
	 *            the dateEvenement to set
	 */
	public void setDateEvenement(DateC dateEvenement) {
		if (dateEvenement.verifDate(dateEvenement.getNumJour(), dateEvenement.getMois(), dateEvenement.getAnnee())) {
			this.dateDepart = dateEvenement;
		}
	}

	/**
	 * @return the creneau
	 */
	public CreneauHoraire getCreneau() {
		return this.creneau;
	}

	/**
	 * @param creneau
	 *            the creneau to set
	 */
	public void setCreneau(CreneauHoraire creneau) {
		if (creneau.estCorrect(creneau.getHeureDebut(), creneau.getHeureFin())) {
			this.creneau = creneau;
		}
	}

	/**
	 * @return String representing recurrent event
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public String getEvenement() throws IOException, FileNotFoundException {
		if (this.getLibelle() != null && this.getPeriodicite() != null
				&& this.getCreneau() != null && this.getDateEvenement() != null && this.getCreneau().estCorrect(this.getCreneau().getHeureDebut(),
						this.getCreneau().getHeureFin())
						&& this.getDateEvenement().verifDate(this.getDateEvenement().getNumJour(),
								this.getDateEvenement().getMois(),
								this.getDateEvenement().getAnnee())) {
			Properties periodeProperty = new Properties();
			InputStream file = new BufferedInputStream(this.getClass().getResourceAsStream(
					"../periodicite.properties"));
			try {
				periodeProperty.load(file);
			} finally {
				file.close();
			}
			String periodicite2 = this.getPeriodicite()
					.toString();
			return "libelle : "
					+ this.libelle
					+ "\n"
					+ "Date : "
					+ this.getDateEvenement().getDate()
					+ "\n"
					+ "Créneau horaire : "
					+ this.getCreneau().getCreneau()
					+ "\n"
					+ "Périodicité : "
					+ periodeProperty.getProperty(periodicite2);
		}
		return null;
	}

}